class ArtistDate < ActiveRecord::Base
  belongs_to :artist
  belongs_to :full_date

  #Constants
  DATE_EVENTS = %w(Birth Death Formed Disbanded Hiatus-Start Hiatus-End)
  PERSON_EVENTS = %w{Birth Death}
  GROUP_EVENTS = %w{Formed Disbanded Hiatus-Start Hiatus-End}
  
  #Validations
  validates_inclusion_of :event,
                         :in => DATE_EVENTS,
                         :message => "Not a valid date event."
  validates_presence_of :artist, :full_date
  validate :valid_date?
  
  def day
    return full_date.day
  end
  
  def month
    return full_date.month
  end
  
  def year
    return full_date.year
  end
  
  def date_string
    return full_date
  end
  
  def date_string_with_month
    return full_date.with_month
  end
  
  
  private

  #Ensure that an artist only has one birth, death, etc.
  def valid_date?
  
    #Ensures only one birth date
    if event == 'Birth'
	  artist.artist_dates.each do |d|
		errors.add_to_base("Artist can only have one birth date.") if (d.event == 'Birth' && d != self)
	  end
	end
	  
	#Ensures only one death date
	if event == 'Death'
	  artist.artist_dates.each do |d|
        errors.add_to_base("Artist can only have one death date.") if (d.event == 'Death' && d != self)
	  end
	end
	
	#Checks that an band can't be born, etc
	if artist.is_group?
	  PERSON_EVENTS.each do |e|
	    errors.add_to_base("A band can not have a #{event} event") if event == e
	  end
	else
	  GROUP_EVENTS.each do |e|
		errors.add_to_base("A person can not have a #{event} event") if event == e
	  end
	end
	
  end
end
